package layer.versioning;

import lights.interfaces.IField;
import lights.interfaces.ITuple;
import lime.LimeTupleID;

public class VersionedTuple{
	
	private LimeTupleID tupleID;
	private LimeTupleVersion tupleVersion;
	private ITuple tuple;
	
	public VersionedTuple(){
	}
	
	public VersionedTuple(ITuple tuple){
		// get tupleID
		IField tupleIDField = tuple.get(tuple.length()-1);
		tuple = tuple.removeAt(tuple.length()-1);
		tupleID = (LimeTupleID) tupleIDField.getValue();
		// get versNum
		IField versNumField = tuple.get(tuple.length()-1);
		tupleVersion = (LimeTupleVersion) versNumField.getValue();
		this.tuple = tuple.removeAt(tuple.length()-1);
	}
	
	public VersionedTuple(long n){
		this.tupleID = new LimeTupleID(n);
		this.setTupleVersion(new LimeTupleVersion());
	}
	
	public VersionedTuple(LimeTupleID tupleID, LimeTupleVersion versNum, ITuple tuple) {
		super();
		this.tupleID = tupleID;
		this.setTupleVersion(versNum);
		this.tuple = tuple;
	}

	public LimeTupleID getTupleID() {
		return tupleID;
	}

	public void setTupleID(LimeTupleID tupleID) {
		this.tupleID = tupleID;
	}

	public void setTuple(ITuple tuple) {
		this.tuple = tuple;
	}

	public ITuple getTuple() {
		return tuple;
	}

	public void setTupleVersion(LimeTupleVersion tupleVersion) {
		this.tupleVersion = tupleVersion;
	}

	public LimeTupleVersion getTupleVersion() {
		return tupleVersion;
	}
	
	public void updateVersion(){
		tupleVersion.setVersNum(tupleVersion.getVersNum()+1);
	}
}
